<?php

declare(strict_types=1);

namespace App\CustHub\Controller\Permission;

use App\CustHub\Annotation\ApiAuth;
use App\CustHub\Annotation\ChOperationLog;
use App\CustHub\Annotation\ChPermission;
use App\CustHub\Annotation\VerifySignature;
use App\CustHub\Controller\BaseController;
use App\CustHub\Request\SubUserRequest;
use App\CustHub\Request\UserRequest;
use App\CustHub\Service\ChSubUserService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\PostMapping;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;

#[Controller(prefix: 'ch/permission/subuser'),ApiAuth,VerifySignature]
class SubUserController extends BaseController
{
    #[Inject]
    protected ChSubUserService $service;

    /**
     * 用户列表.
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('list'), ChPermission('ch:permission:subuser, ch:permission:subuser:list')]
    public function index(): ResponseInterface
    {
        return $this->success($this->service->getPageList($this->request->all(), false));
    }

    /**
     * 创建一个用户.
     * @param SubUserRequest $request
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('save'), ChOperationLog]
    public function save(SubUserRequest $request): ResponseInterface
    {
        return $this->success(['id' => $this->service->save($request->all())]);
    }

    #[PostMapping('getInfo'), ChOperationLog]
    public function getInfo(SubUserRequest $request): ResponseInterface
    {
        return $this->success($this->service->getInfo($request->all()));
    }

    /**
     * 更新一个用户信息.
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('update'), ChOperationLog]
    public function update(SubUserRequest $request): ResponseInterface
    {
        return $this->service->update($request->input('id'), $request->all()) ? $this->success() : $this->error();
    }

    /**
     * 删除用户
     * @return ResponseInterface
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('delete'), ChOperationLog]
    public function delete(SubUserRequest $request): ResponseInterface
    {
        return $this->service->realDelete([$request->input('id')]) ? $this->success() : $this->error();
    }

    #[PostMapping('setGroup'), ChOperationLog]
    public function setGroup(SubUserRequest $request): ResponseInterface
    {
        return $this->service->setGroup($request->all()) ? $this->success() : $this->error();
    }

    /**
     * 修改密码
     * @param SubUserRequest $request
     * @return \Psr\Http\Message\ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('modifyPassword')]
    public function modifyPassword(SubUserRequest $request): \Psr\Http\Message\ResponseInterface
    {
        return $this->service->modifyPassword($request->validated()) ? $this->success() : $this->error();
    }
}